/**************************************************************
 * encoding_convert.h - 
 * Copyright (c) 2009  Qin Xiaoxue  <crs.chin@gmail.com>
 *
 * This file is  free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
**************************************************************/


#ifndef __ENCODING_CONVERT_H
#define __ENCODING_CONVERT_H

#include <stdio.h>
#include <enca.h>
#include <glib.h>


/*
 *convert file @fp from encoding @from to @to, return the converted
 *file pointer when encountered invalid char seuence if @keepon if
 *true, else return NULL
 **/
// FILE    *encoding_convert_const(FILE *fp, const gchar *from, const gchar *to, gboolean keepon);  /* depreciated */
FILE    *encoding_convert_to(FILE *fp_from, FILE *fp_to, const gchar *from, const gchar *to, gboolean keepon);

#define encoding_convert_const(fp, from, to, keepon)	\
  ({													\
	FILE *tmp = tmpfile();								\
	encoding_convert_to(fp, tmp, from, to, keepon);		\
  })


/*
 *convert file @fp from encoding @from to @to, return ok when
 *encountered invalid char seuence if @keepon if true, else return
 *NULL
 *
 *NOTE. @fp must be writable, it could be truncated.
 **/
gboolean  encoding_convert(FILE *fp, const gchar *from, const gchar *to, gboolean keepon);

const gchar  *guess_encoding(const gchar *buf, size_t size, const gchar *lang, int namestyle);

/*
 * guess the encoding from buf using libenca, and convert it into
 * encoding @to and return it, if @out is not NULL, the returned
 * string length is written to it, set @size to -1 if @buf is nul
 * terminated
 */
gchar   *zh_convert(const gchar *buf, size_t size, size_t *out, const gchar *to);


#endif /* __ENCODING_CONVERT_H */

